Solving 10385 - Duathlon (Ternary search)
[andmenj-acm.git] / 12319 - Edgetown's Traffic Jams / 12319.cpp
blobb2294e61367e407484ddf9c01240c898427055b7
1 // Equipo Poncho, Carriel y Ruana
2 using namespace std;
3 #include <algorithm>
4 #include <iostream>
5 #include <iterator>
6 #include <sstream>
7 #include <fstream>
8 #include <cassert>
9 #include <climits>
10 #include <cstdlib>
11 #include <cstring>
12 #include <string>
13 #include <cstdio>
14 #include <vector>
15 #include <cmath>
16 #include <queue>
17 #include <stack>
18 #include <list>
19 #include <map>
20 #include <set>
22 template <class T> string toStr(const T &x)
23 { stringstream s; s << x; return s.str(); }
24 template <class T> int toInt(const T &x)
25 { stringstream s; s << x; int r; s >> r; return r; }
27 #define For(i, a, b) for (int i=(a); i<(b); ++i)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " = " << (x) << endl;
31 const double EPS = 1e-9;
33 int cmp(double x, double y = 0, double tol = EPS) {
34 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
37 const int MAXN = 105;
38 const int oo = 1<<28;
40 int g1[MAXN][MAXN];
41 int g2[MAXN][MAXN];
42 int n;
44 vector<int> gg1[MAXN], gg2[MAXN];
46 int dist[MAXN];
47 void bfs(int start, vector<int> g[MAXN]) {
48 For(i, 0, n) dist[i] = oo;
49 queue<int> q;
50 dist[start] = 0;
51 q.push(start);
52 while (q.size()) {
53 int u = q.front();
54 q.pop();
56 for (int k = 0; k < g[u].size(); ++k) {
57 int v = g[u][k];
59 if (dist[v] < oo) continue;
60 dist[v] = dist[u] + 1;
61 q.push(v);
66 bool equal(long long a, long long b) {
67 For(i, 0, n) {
68 For(j, 0, n) {
69 if (g2[i][j] >= oo) return false;
70 long long now = g2[i][j];
71 long long cota = g1[i][j] * a + b;
72 if (now > cota) {
73 //printf("From %d to %d: now = %d, cota = %d, old = %d\n", i, j, (int)now, (int)cota, g1[i][j]);
74 return false;
78 return true;
81 int main(){
82 while (cin >> n) {
83 if (n == 0) break;
84 string s; getline(cin, s);
86 For (i, 0, n) For(j, 0, n) g1[i][j] = g2[i][j] = oo;
87 For (i, 0, n) gg1[i].clear(), gg2[i].clear();
89 for (int i = 0; i < n; ++i) {
90 getline(cin, s);
91 stringstream sin(s);
92 int first; sin >> first; first--;
93 int other;
94 while (sin >> other) {
95 other--;
96 g1[first][other] = 1;
97 gg1[first].push_back(other);
101 for (int i = 0; i < n; ++i) {
102 getline(cin, s);
103 stringstream sin(s);
104 int first; sin >> first; first--;
105 int other;
106 while (sin >> other) {
107 other--;
108 g2[first][other] = 1;
109 gg2[first].push_back(other);
112 For(i, 0, n) g1[i][i] = g2[i][i] = 0;
114 int a, b;
115 cin >> a >> b;
116 if (a == 0 and b == 0 and n > 1) {
117 puts("No");
118 continue;
121 //puts("G1"); For(i, 0, n){ For(j, 0, n) { printf("%10d", g1[i][j]); } puts(""); } puts("");
122 //puts("G2"); For(i, 0, n){ For(j, 0, n) { printf("%10d", g2[i][j]); } puts(""); } puts("");
124 //floyd();
126 For(i, 0, n) {
127 bfs(i, gg1);
128 For(j, 0, n) {
129 g1[i][j] = dist[j];
131 bfs(i, gg2);
132 For(j, 0, n) {
133 g2[i][j] = dist[j];
137 if (equal(a, b)) {
138 puts("Yes");
139 } else {
140 puts("No");
143 return 0;